/*
 *	INFO :
 *		the file is to get node from balance server
 *
 *	AUTHOR:
 *		Mark on 2014/3/19
 */

/*
 *code log:
 *
 *
 */
#include"balance.h"
#include"client_connect.h"
#include<stdio.h>
#include<stdlib.h>

#ifndef SINGLE_DEBUF_MODE
#define SINGLE_DEBUG_MODE 1
#endif

/*
 *this struct is the main strut
 */
extern struct core_conf *Core_conf;
/*
 * the sturct record the legal node that registered in this server
 */
extern struct client_connect *CC;
/*
 * this struct record the info get from balance
 */
extern struct sockaddr_in *need_dispatched_client;

/*
 *is to parse the buffer get from balance to the choosen client 
 *need add function
 */

struct sockaddr_in * parse_buffer(char *buffer)
{
	return (struct sockaddr_in *)buffer; 
}
/*
 *is to judge if the addr parsed is legal
 *need add function
 */

int is_legal(struct sockaddr_in * recv)
{
	return 1;
}

/*
 *this main function of dispatch
 *
 */

void * get_dispathed()
{
	struct sockaddr_in *addr;

	int s,len=128;
	int i=0;	
	int addr_len =sizeof(struct sockaddr_in);
	char buffer[2000];

//	s=Core_conf->sock_balance;
	s=socket(AF_INET,SOCK_DGRAM,0);

	addr=Core_conf->balance_addr;

	bzero(buffer,sizeof(buffer));

	#if SINGLE_DEBUG_MODE
	need_dispatched_client=CC->head[0].Node;

	#else
        sendto(s,"nidaye",7,0,(struct sockaddr *)addr,addr_len);	

	printf("have sended\n");
	/* 接收server端返回的字符串*/
	len = recvfrom(s,buffer,sizeof(buffer),0,(struct sockaddr *)addr,&addr_len);

	//this parse the send context
	
	addr=parse_buffer(buffer);
	
	printf("receive:%d\n",sizeof(buffer));

	for(i=0;i<16;i++)
	{
		printf("0x%x",buffer[i]);
	}
	printf("             ");
	
	if(is_legal(addr))
	{
		need_dispatched_client=addr;
	}

	#endif

}

